/**
  ******************************************************************************
  * @file    IAP_Main/Inc/flash_if.h 
  * @author  MCD Application Team
  * @version 1.0.0
  * @date    8-April-2015
  * @brief   This file provides all the headers of the flash_if functions.
  * @note    2023/8/19/张剑勇。本文件为 STM32L031G6U6 进行了修改。
  ******************************************************************************
  * @attention
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __FLASH_IF_H
#define __FLASH_IF_H

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* 应用程序入口地址,最好从扇区对齐地址开始,非扇区对齐未测试 */
#define APPLICATION_ENTRY_ADDRESS     ADDR_FLASH_SECTOR_2
/* 用户应用程序结尾地址 一般以片内flash结尾 */
#define APPLICATION_END_ADDRESS       FLASH_END_ADDR

/* 获取本芯片flash大小(KB),移植前需根据手册核对读取地址 */
#define FLASH_SIZE_KB           (*(volatile uint16_t *)0x1FFF7A22)
#define FLASH_SIZE_BYTE         (FLASH_SIZE_KB * 1024)
#define FLASH_END_ADDR          (FLASH_BASE + FLASH_SIZE_BYTE -1)

/* 移植前需要根据手册确定扇区 */
#define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) /* Base @ of Sector 0,  16  Kbytes */
#define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) /* Base @ of Sector 1,  16  Kbytes */
#define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) /* Base @ of Sector 2,  16  Kbytes */
#define ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000) /* Base @ of Sector 3,  16  Kbytes */
#define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000) /* Base @ of Sector 4,  64  Kbytes */
#define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000) /* Base @ of Sector 5,  128 Kbytes */
#define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000) /* Base @ of Sector 6,  128 Kbytes */
#define ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000) /* Base @ of Sector 7,  128 Kbytes */
#define ADDR_FLASH_SECTOR_8     ((uint32_t)0x08080000) /* Base @ of Sector 8,  128 Kbytes */
#define ADDR_FLASH_SECTOR_9     ((uint32_t)0x080A0000) /* Base @ of Sector 9,  128 Kbytes */
#define ADDR_FLASH_SECTOR_10    ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
#define ADDR_FLASH_SECTOR_11    ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
//#define ADDR_FLASH_SECTOR_12    ((uint32_t)0x08100000) /* Base @ of Sector 12, 128 Kbytes */
//#define ADDR_FLASH_SECTOR_13    ((uint32_t)0x08120000) /* Base @ of Sector 13, 128 Kbytes */
//#define ADDR_FLASH_SECTOR_14    ((uint32_t)0x08140000) /* Base @ of Sector 14, 128 Kbytes */
//#define ADDR_FLASH_SECTOR_15    ((uint32_t)0x08160000) /* Base @ of Sector 15, 128 Kbytes */

enum 
{
  FLASHIF_OK = 0,
  FLASHIF_ERASEKO,
  FLASHIF_WRITINGCTRL_ERROR,
  FLASHIF_WRITING_ERROR,
  FLASHIF_PROTECTION_ERRROR
};

/* protection type */  
enum{
  FLASHIF_PROTECTION_NONE         = 0,
  FLASHIF_PROTECTION_PCROPENABLED = 0x1,
  FLASHIF_PROTECTION_WRPENABLED   = 0x2,
  FLASHIF_PROTECTION_RDPENABLED   = 0x4,
};

/* protection update */
enum {
	FLASHIF_WRP_ENABLE,
	FLASHIF_WRP_DISABLE
};

// [zjy 20230819]
/* Define the address from where user application will be loaded.
   Note: this area is reserved for the IAP code                  */
#define FLASH_PAGE_STEP         FLASH_PAGE_SIZE           /* Size of page : 128bytes */


/* Notable Flash addresses */
#define FLASH_START_BANK1             ((uint32_t)0x08000000)
#define WORDS_IN_HALF_PAGE            ((uint32_t)16)
#define FLASH_HALF_PAGE_SIZE          ((uint32_t)WORDS_IN_HALF_PAGE*4)

//#define FLASH_LAST_SECTOR_BANK1       ((uint32_t)0x08017000)
#define FLASH_LAST_SECTOR_BANK1       ((uint32_t)0x08007000)

//#define FLASH_LAST_PAGE_BANK1         ((uint32_t)0x08017F00)
#define FLASH_LAST_PAGE_BANK1         ((uint32_t)0x08007F80)

//#define FLASH_END_BANK1               ((uint32_t)0x08017FFF)
#define FLASH_END_BANK1               ((uint32_t)0x08007FFF)

#define FLASH_START_BANK2             ((uint32_t)0x08018000)

//#define USER_FLASH_END_ADDRESS        ((uint32_t)0x08030000)
#define USER_FLASH_END_ADDRESS        ((uint32_t)0x08008000)

#define FLASH_BANK1_MASK              ((uint32_t)0xF0FFFFFF)

/* Define the user application size */
#define USER_FLASH_SIZE               ((uint32_t)0x00006000) /* Small default template application */

/* Exported functions ------------------------------------------------------- */
void FLASH_If_Init(void);
uint32_t FLASH_If_Erase(uint32_t StartSector);
uint32_t FLASH_If_GetWriteProtectionStatus(void);
uint32_t FLASH_If_Write(uint32_t destination, uint32_t *p_source, uint32_t length);
//uint32_t FLASH_If_WriteProtectionConfig(uint32_t protectionstate);

#endif  /* __FLASH_IF_H */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
